package com.bangdream.michelia.view.fragment.main.curriculum;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;

import com.bangdream.michelia.R;
import com.bangdream.michelia.contract.CoursesContract;
import com.bangdream.michelia.entity.CommentBean;
import com.bangdream.michelia.entity.RxEventBean;
import com.bangdream.michelia.entity.courses.CoursesBean;
import com.bangdream.michelia.entity.CoursesTypeBean;
import com.bangdream.michelia.presenter.CoursesPresenter;
import com.bangdream.michelia.utils.L;
import com.bangdream.michelia.utils.RxBus;
import com.bangdream.michelia.view.activity.currency.MainActivity;
import com.bangdream.michelia.view.activity.currency.OnMainActivityOperationListener;
import com.bangdream.michelia.view.fragment.currency.BaseFragment;
import com.bangdream.michelia.view.fragment.main.SlideRightFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * @date on 2018/6/18 13:19
 * @email 909732184@qq.com
 * 课程
 */
public class MainCurriculum extends BaseFragment<CoursesContract.ICoursesView,CoursesPresenter<CoursesContract.ICoursesView>>
    implements CoursesContract.ICoursesView{
    private View view;
    private View statusBarLayout;
    private TabLayout tabLayout;
    private ViewPager viewPage;
    private ImageView iconToBottom;
    private EditText editSearch;
    private List<String> titles = new ArrayList<>();
    //课程分类一级列表
    private List<CoursesTypeBean> dataList;
    private List<CurriculumViewPageItem> fragmentList = new ArrayList<>();
    private OnMainActivityOperationListener onMainActivityOperationListener;

    public OnMainActivityOperationListener getOnMainActivityOperationListener() {
        return onMainActivityOperationListener;
    }

    public void setOnMainActivityOperationListener(OnMainActivityOperationListener onMainActivityOperationListener) {
        this.onMainActivityOperationListener = onMainActivityOperationListener;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.main_item_curriculum,container,false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        setListener();
        operateBus();
    }

    private void setListener() {
        iconToBottom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onMainActivityOperationListener!=null){
                    onMainActivityOperationListener.onOpenSlide(true);
                }
            }
        });
//        editSearch.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                if(viewPage==null||viewPage.getAdapter()==null||viewPage.getAdapter().getCount()==0)
//                    return;
//                fragmentList.get(viewPage.getCurrentItem()).searchList(s.toString());
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//
//            }
//        });
    }

    private void initView() {
        initTitleBar("","课程","",null);
        editSearch = view.findViewById(R.id.editSearch);
        statusBarLayout = view.findViewById(R.id.statusBarLayout);
        ViewGroup.LayoutParams layoutParams = statusBarLayout.getLayoutParams();
        layoutParams.height = MainActivity.statusBarHeight;
        statusBarLayout.setLayoutParams(layoutParams);
        tabLayout = view.findViewById(R.id.tabLayout);
        viewPage = view.findViewById(R.id.viewPage);
        iconToBottom = view.findViewById(R.id.iconToBottom);

        mPresenter.getCoursesTypeListData(null);

//        dataList = dbHelper.queryAll(daoInstancs.getCoursesTypeBeanDao());
//        setView();

    }


    @SuppressLint("CheckResult")
    public void operateBus() {
        RxBus.getInstance().toObservable().map(o -> (RxEventBean) o).subscribe(bean -> {
            if(bean.getCode()==0x02){
                mPresenter.getCoursesTypeListData(null);

            }
        });
    }
    //设置ViewPage和TabLayout
    private void setView(){
        titles.clear();
        fragmentList.clear();
        viewPage.removeAllViewsInLayout();
//        if(dataList==null||dataList.size()==0)
//            return;

        if(!dataList.isEmpty())
        for(int i = 0;i<dataList.size()+1;i++){
            if(i==0 ){
                titles.add("全部");
            }else {
                titles.add(dataList.get(i-1).getName());
            }
        }
        /**
         * 初始化Adapter
         */
        FragmentStatePagerAdapter mAdapter = new FragmentStatePagerAdapter(getChildFragmentManager()) {
            @Override
            public int getCount() {
                return  titles==null?0:titles.size();
            }
            @Override
            public Fragment getItem(int arg0) {
                CurriculumViewPageItem item = new CurriculumViewPageItem();
                if(arg0>0) {
                    Bundle b = new Bundle();
                    b.putSerializable("typeData", dataList.get(arg0 - 1));
                    item.setArguments(b);
                }
                fragmentList.add(item);
                return item;
            }
            @Override
            public CharSequence getPageTitle(int position) {
                return titles.get(position);
            }
        };
        viewPage.setAdapter(mAdapter);
        //总页数和10比较取小 设置缓存页数
        viewPage.setOffscreenPageLimit(Math.min(mAdapter.getCount(),10));
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);//设置tab模式，当前为系统默认模式
        tabLayout.setupWithViewPager(viewPage);//将TabLayout和ViewPager关联起来。

    }
    @Override
    public CoursesPresenter createPresenter() {
        CoursesPresenter presenter = new CoursesPresenter();
        return presenter;
    }

    @Override
    public void setCoursestList(boolean isSuccess, int page, CoursesContract.CoursesType type, List<CoursesBean> data, String stDescribe) {

    }

    @Override
    public void setCoursesDetails(boolean isSuccess, CoursesBean data, String stDescribe) {

    }

    @Override
    public void setCoursesTypeList(boolean isSuccess, List<CoursesTypeBean> data, String stDescribe) {
        dbHelper.deleteDataAll(daoInstancs.getCoursesTypeBeanDao());
        dbHelper.insertDataList(daoInstancs.getCoursesTypeBeanDao(),data);

//        if(dataList==null||dataList.size()==0){
            dataList = data;
            setView();
//        }
    }

    @Override
    public void setCommentList(boolean isSuccess, int page, List<CommentBean> data, String stDescribe) {

    }

    @Override
    public void actionComment(boolean isSuccess, String stDescribe) {

    }

    public void setItemSwitch(int position, CoursesTypeBean bean) {
        if(viewPage!=null){
            viewPage.setCurrentItem(position+1);
        }
    }
}
